//! 节点类型
//!
//!

use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[repr(u8)]
pub enum NodeType {
    Seed = 0,
    FullNode = 1,
    LocalFullNode = 2,
    RelayNode = 3,
}

impl Default for NodeType {
    fn default() -> Self {
        Self::Seed
    }
}

impl From<u8> for NodeType {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Seed,
            1 => Self::FullNode,
            2 => Self::LocalFullNode,
            3 => Self::RelayNode,
            _ => panic!("unsupported node type"),
        }
    }
}

impl Into<u8> for NodeType {
    fn into(self) -> u8 {
        self as u8
    }
}

impl Display for NodeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NodeType::Seed => f.write_str("seed"),
            NodeType::FullNode => f.write_str("fullnode"),
            NodeType::LocalFullNode => f.write_str("localfullnode"),
            NodeType::RelayNode => f.write_str("relaynode"),
        }
    }
}
